package czk.concrent.juc.semaphore;



import java.util.concurrent.Semaphore;

/**
 * 限制线程数
 * @Author:ChenZhangKun
 * @Date: 2020/7/24 23:04
 */
public class SemaphoreDemo {
    public static void main(String[] args) {
        // 创建允许的线程数
        Semaphore semaphore=new Semaphore(3);
        // 开始线程
        for (int i = 0; i < 10; i++) {
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

                new Thread(()->{
                    System.out.println(System.currentTimeMillis()+"  开始");
                    try {
                        Thread.sleep(100);
                        System.out.println(System.currentTimeMillis()+"  结束");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {
                        semaphore.release();
                    }
                }).start();


        }
    }
}
